<?php
/**
 * settings.inc
 * Web interface to edit local and global settings in ini files. 
 * @copyright	Copyright(c) 2002-2010 Bjorn Winberg
 * @author		Bjorn Winberg <cms@anomalye.net>
 * @license		http://www.gnu.org/licenses/gpl.html GNU General Public License version 2 
 * @package		AnomalyWiki
 */
class Settings extends Module{
	public static $MODULE_UID = 'settings';
	const PARAM_MAX_ACTION = 64;
	const PARAM_MAX_MODULE = 64;
	const PARAM_MAX_VALUE = 20000;
	const LOCAL_SECTION = 'localized';
	const GLOBAL_SECTION = 'settings';
	protected $mSystem;
	
	/**
	 *
	 */
	public function __construct(){
		parent::__construct();
		$this->loadSettings();
		$this->mSystem = ModuleFactory::loadModule('system');
	}
	
	/**
	 *
	 */
	public function generateView(){
		if(!$this->requireAuthorization('SETTINGS/ADMIN')){
			$this->mSystem->feedbackWarn($this->mSystem->localString('ERROR_PERMISSION_DENIED'));
			return false;
		}
		
		$inputAction = $this->getParamEnc('a', 0, self::PARAM_MAX_ACTION);
		$inputModule = $this->getParamEnc('module', 0, self::PARAM_MAX_MODULE, 'system');
		$localIniPath = $this->getLocalIniFile($inputModule);
		$globalIniPath = $this->getGlobalIniFile($inputModule);
		
		$params['module'] = $inputModule;
		$params['fileList'] = $this->getModules(ModuleFactory::getProperty('system', 'INI_FULLDIR'));
		
		foreach($params['fileList'] as $id => $name){
			$params['fileList'][$id] = ModuleFactory::getString($id, 'MODULE_NAME');
		}
		
		
		 
		
		
		$params['localList'] = parse_ini_file($localIniPath, true);
		$params['globalList'] = parse_ini_file($globalIniPath, true);
				
		if($inputAction === 'save'){
			$params['localList'] = $this->saveSettings($params['module'], $localIniPath, $params['localList'], self::LOCAL_SECTION);
			$params['globalList'] = $this->saveSettings($params['module'], $globalIniPath, $params['globalList'], self::GLOBAL_SECTION);
			$this->mSystem->feedbackInfo($this->localString('OK_SAVE'));
		}
	
		return $this->generateTemplate('settings_list.inc', $params);
	}
	
	/**
	 *
	 */
	public function firstTimeSetup(){
		$db = ModuleFactory::getDb();
		$auth = ModuleFactory::loadModule('auth');
		$permissionId = $auth->addAction($db, rawurlencode('SETTINGS/ADMIN'), 1);
		$auth->grantAction($db, $auth::ADMIN_GROUP_ID, $permissionId);
	}
	
	/**
	 *
	 */
	private function getModules($aFullDir){
		$ret = array();
		$searchPattern = $aFullDir . DIRECTORY_SEPARATOR . '*.ini';
		foreach(glob($searchPattern) as $path){
			if(!is_dir($path)){
				$pathBits = explode(DIRECTORY_SEPARATOR, $path);
				$filename = array_pop($pathBits);
				$nameBits = explode('.', $filename);
				$ret[rawurlencode($nameBits[0])] = '';
			}
		}
		return $ret;
	}
	
	/**
	 *
	 */
	public function saveSettings($aModule, $aFile, $aSettingsList, $aSection){
		$outStr = '';
		foreach($aSettingsList as $inSection => $inSettings){
			if($inSection === $aSection){
				foreach($inSettings as $inKey => $oldValue){
					$inputValue = $this->getParamEnc($inKey, 0, self::PARAM_MAX_VALUE);
					if($inputValue !== false){
						if($aSettingsList[$inSection][$inKey] != $inputValue){
							$aSettingsList[$inSection][$inKey] = $inputValue;
							$this->mSystem->feedbackLog(static::$MODULE_UID, $this->localString('LOG_SAVE', array($aModule, $inKey)));
						}
					}
					else{
						$this->mSystem->feedbackWarn($this->localString('ERROR_TOO_LONG', array($inKey)));
					}
				}
			}
		}
		foreach($aSettingsList as $outSection => $outSettings){
			$outStr .= '[' . $outSection . "]\r\n";
			foreach($outSettings as $outKey => $outValue){
				$outStr .= "\t" . $outKey . ' = "' . $outValue . "\"\r\n";
			}
		}
		file_put_contents($aFile, $outStr);
		return $aSettingsList;
	}
}
?>
